package hcdh.java.donthuc;

import java.util.Scanner;

public class DonThuc implements Comparable<DonThuc>{
	private double m_a;
	private int m_n;
	
	public DonThuc() {
    }

	public DonThuc(double a, int n) {
	    setA(a);
	    setN(n);
    }
	
	public static void main(String[] args)
    {
        //DonThuc dt = new DonThuc();
        //dt.nhap();
        
//        System.out.print("Nhap x0 = ");
//        
//        Scanner scaner = new Scanner(System.in);
//        System.out.print("Ket qua " + dt + " : " + dt.tinh(scaner.nextDouble()));
        
        DonThuc dt1 = DonThuc.parse("-5.3ymu4", "y", "mu");
        System.out.println(dt1);
    }

	public void setA(double m_a) {
		this.m_a = m_a;
	}

	public double getA() {
		return m_a;
	}

	public void setN(int m_n) {
		this.m_n = m_n;
	}

	public int getN() {
		return m_n;
	}
	
	public double tinh(double x) {
		double s = 1;
		for (int i = 1; i <= getN(); i++) {
			s *= x;
		}
		return m_a * s;
	}
	
	public void nhap() {
		Scanner scan = new Scanner(System.in);
		
		System.out.print("a : ");
		m_a = scan.nextDouble();
		
		System.out.print("n : ");
		m_n = scan.nextInt();
	}
	
	public void xuat() {
		this.toString();
	}
	
	public static void xuat(DonThuc dt) {
		dt.toString();
	}
	
	public String toString() {
		return getA() + "x^" + getN();
	}
	
	public static DonThuc parse(String val, String x, String mu) {
	    if (val == null || val.isEmpty() || x == null || x.isEmpty() || mu ==null || mu.isEmpty()) {
	        throw new RuntimeException("Parse don thuc that bai");
	    }
	    String s[] = val.split("[" + x + mu + "]");
	    
	    if (s.length != 4) {
	        throw new RuntimeException("Parse don thuc that bai");
	    }
	    return new DonThuc(Double.parseDouble(s[0]), Integer.parseInt(s[3]));
	}

    public int compareTo(DonThuc dt) {
        return  getN() - dt.getN();
    }
    
    public DonThuc cong(DonThuc dt) {
        if (dt.getN() != this.getN()) {
            return null;
        }
        return new DonThuc(getA() + dt.getA(), getN());
    }
    
    public DonThuc tru(DonThuc dt) {
        if (dt.getN() != this.getN()) {
            return null;
        }
        return new DonThuc(getA() - dt.getA(), getN());
    }
}
